package com.lwm.service.impl;

import cn.hutool.core.util.IdUtil;
import com.lwm.domain.DeliveryOrder;
import com.lwm.domain.ResponseResult;
import com.lwm.domain.SalesReturn;
import com.lwm.domain.SalesReturnDetail;
import com.lwm.dto.PageParams;
import com.lwm.dto.SalesReturnDto;
import com.lwm.mapper.SalesReturnDetailMapper;
import com.lwm.mapper.SalesReturnMapper;
import com.lwm.service.SalesReturnService;
import com.lwm.utils.Code;
import com.lwm.utils.HintMessage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SalesReturnServiceImpl implements SalesReturnService {
    @Resource
    private SalesReturnMapper salesReturnMapper;
    @Resource
    private  SalesReturnDetailMapper salesReturnDetailMapper;
    @Resource
    private DeliveryOrderService deliveryOrderService;
    @Override
    @Transactional
    public ResponseResult save(SalesReturn salesReturn) {
        Long salesReturnId = IdUtil.getSnowflakeNextId();
        SalesReturnDetail salesReturnDetail = salesReturn.getSalesReturnDetail();
        if(salesReturnDetail.getIsDeleted() == null) salesReturnDetail.setIsDeleted(0);
        if(salesReturn.getIsDeleted() == null) salesReturn.setIsDeleted(0);
        salesReturnDetail.setSalesReturnId(salesReturnId);
        salesReturnDetail.setId(IdUtil.getSnowflakeNextId());
        salesReturnDetailMapper.insert(salesReturnDetail);

        salesReturn.setId(salesReturnId);
        salesReturn.setReturnOrderNumber(IdUtil.simpleUUID());
        salesReturnMapper.insert(salesReturn);
        return new ResponseResult(Code.SAVE_OK, HintMessage.SAVE_OK_HINT);
    }

    @Override
    public ResponseResult update(SalesReturn salesReturn) {
        SalesReturnDetail salesReturnDetail = salesReturn.getSalesReturnDetail();
        if(salesReturnDetail.getId() == null || salesReturn.getId() == null) return new ResponseResult(Code.UPDATE_ERR, HintMessage.EDIT_ERR_HINT);
        salesReturnMapper.updateById(salesReturn);
        salesReturnDetailMapper.update(salesReturnDetail);
        return new ResponseResult(Code.UPDATE_OK, HintMessage.EDIT_OK_HINT);
    }

    @Override
    public ResponseResult removeBatch(List<Long> ids) {
        salesReturnDetailMapper.removeBatchBySalesReturnId(ids);
        salesReturnMapper.removeBatch(ids);
        return new ResponseResult(Code.DELETE_OK, HintMessage.REMOVE_OK_HINT);
    }

    @Override
    public ResponseResult getPage(SalesReturnDto salesReturnDto) {
        if(salesReturnDto.getSize() == null) salesReturnDto.setSize(10);
        if(salesReturnDto.getPage() == null) salesReturnDto.setPage(1);
        // 拿到总条数
        Long total = salesReturnMapper.getTotal(salesReturnDto);
        // 拿到最大页码
        Double maxPage = Math.ceil(Double.valueOf(total) / Double.valueOf(salesReturnDto.getSize()));
        // 求当前页码
        Integer currentPage = maxPage.intValue() < salesReturnDto.getPage() ? maxPage.intValue() : salesReturnDto.getPage();
        currentPage = currentPage <= 0 ? 1 : currentPage;
        // 求开始的页码
        Integer startPage = (currentPage - 1) * salesReturnDto.getSize();
        salesReturnDto.setPage(startPage);

        List<SalesReturn> pageList = salesReturnMapper.getPage(salesReturnDto);

        pageList.forEach(item -> {
            SalesReturnDetail bySalesReturnId = salesReturnDetailMapper.getBySalesReturnId(item.getId());
            item.setSalesReturnDetail(bySalesReturnId);
            DeliveryOrder byId = deliveryOrderService.getById(item.getDeliveryOrderId());
            item.setDeliveryOrder(byId);
        });

        PageParams<Object> objectPageParams = new PageParams<>(currentPage, salesReturnDto.getSize(), total.intValue(), pageList);
        return new ResponseResult(Code.GET_OK, HintMessage.GET_OK_HINT, objectPageParams);
    }
}
